#include <p30f4013.h>
#include <stdlib.h>
#include <string.h>
#include "Delays.h"
#include "LCD_controls.h"

//--------------------------------------------------------------------------
//		LCD controls
//--------------------------------------------------------------------------

extern unsigned char current_addr;

/*
	INPUT: none
	OUTPUT: Toggles the LCD enable pin on the LCD to signal
			that an instruction should now be processed by the LCD
*/
void LCDenable(void)
{
    unsigned char i;

    delay_50uS();   //put this in here to properly meet setup times?

    LCDENABLE = 1;

    //this loop is just to keep LCDENABLE high for a certain amount of time
    for(i=0; i<10; i++)
    {
	
	asm("nop");
	asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
	asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
	asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
	asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
        asm("nop");
	
    }

    LCDENABLE = 0;

	delay_50uS();

	return;
}



/*
	INPUT: Array of characters and an index value
	OUTPUT: If the index is non-negative, the character in the array
			at the given index is printed.  If the index is negative
			then the entire array is printed.
	ERROR CHECK: If the index given is outside the range of the given array
				 then the function simply exits
*/

void LCDwrite(char output[])
//void LCDwrite(char* output)
{
	unsigned char i;
	LCDRS = 1;

//If index is less than zero, print the whole array

		for(i = 0; output[i] != '\0'; i++)
		{
			LCD_DATA = output[i];
			LCDenable();
			current_addr++;
		}


	return;
}


/*
void LCDwrite(char output[], char index)
{
	unsigned char i;
	LCDRS = 1;

//If index is less than zero, print the whole array
	if(index < 0)
	{	
		for(i = 0; output[i] != '\0'; i++)
		{
			LCD_DATA = output[i];
			LCDenable();
			current_addr++;
		}
	}

//otherwise just print the character at the given index within the array
	else if(index >= 0 && index < strlen(output))
	{
		LCD_DATA = output[index];
		LCDenable();
		current_addr++;
	}

//If the given index is out of the bounds of the array, exit the function
	else
	{
		return;
	}


	return;
}


*/





/*
	INPUT: ASCII character to be printed to LCD
	OUTPUT: Prints the character to the current address of the LCD
			 and increments the current address.
*/
void LCDchar(char character, char auto_increment)
{
	LCDRS = 1;
	LCD_DATA = character;
	LCDenable();

        if(auto_increment)
        {
            current_addr++;
        }

        else
        {
            LCDaddress(current_addr);
        }
        

	return;
}



/*
	INPUT: ASCII character to be printed to LCD
	OUTPUT: Prints the character to the given address of the LCD
			 and resets the current address
*/
void LCDchar_addr(char character, char address)
{

        LCDaddress(address);

        LCDRS = 1;
	LCD_DATA = character;
	LCDenable();

        LCDaddress(address);        //keep the cursor at this address too

        current_addr = address;     //current address is the address given to
                                    // this function, plus one because of
                                    // the auto increment of the LCD


	return;
}





/*
	INPUT: An LCD DDRAM address. Bit 7 must always be set to 1	
	OUTPUT: Changes the display position to the corresponding input address
			and returns the current address
*/
void LCDaddress(unsigned char addr)
{
	LCDRS = 0;

	LCD_DATA = addr;
	LCDenable();
	current_addr = addr;

	return;
}


/*
	INPUT: Current display address
	OUTPUT: Decrements the current address by one and
			 deletes the character at that position
*/
void backspace(unsigned char addr)
{
	LCDRS = 1;
	LCDaddress(addr-1);
	LCD_DATA = ' ';
	LCDenable();
	LCDaddress(addr-1);		//Once the character at addr-1 is erased,
							// the LCD auto increments the display RAM 
							// address, so I need to reset it to the
							// correct "backspace" postion.

	return;
}



/*
	INPUT: none
	OUTPUT: Clears the entire LCD screen
*/
void LCDclear(void)
{
	LCDRS = 0;			//This command code is given in the 
	LCD_DATA = 1;			// datasheet on page 24
	LCDenable();	
	current_addr = LINE1;

	milliseconds(2);

	return;
}


/*
	INPUT: none
	OUTPUT: Puts the LCD cursor back to the home position, the upper left most spot
*/
void LCDhome(void)
{
	LCDRS = 0;			//This command code is given in the 
	LCD_DATA = 2;			// datasheet on page 24
	LCDenable();

	return;
}	
